<template>
    <view class="homemakingitem item">
        <view class="store flexb">
            <view class="store_name flexb">
                <image class="icon" :src="imgpath + 'store_icon.png?v=' + imgVersion" mode="aspectFit" > </image>
                <view class="txt">{{ objInfo.company }}</view>
            </view>
            <view class="order_state" >{{getState(objInfo)}}</view>
        </view>
        <view class="order-content" @click="toLink('order')">
            <view class="order-info flexbox">
                <view class="flexl order_detail flexbox">
                    <view class="pic loadingImg">
                        <image
                            :src="objInfo.product.litpic "
                            mode="aspectFill"
                        />
                    </view>
                    <view class="detail">
                        <view class="h4">{{ objInfo.product.title }}</view>
                        <view class="info">
                            <view :class="{'p':true,'overtime':!['0','1','2','4','5'].includes(objInfo.orderstate)}">预约时间：<text :class="{'intime':['0','1','2','4','5'].includes(objInfo.orderstate)}">{{ timeTrans(objInfo) }}</text></view>
                            <view class="p" v-if="objInfo.orderstate == 11">结算时间：<text>{{ objInfo.statementtime }}</text></view>
                        </view>
                        <view class="free" v-if="objInfo.homemakingtype == 0">免费预约</view>
                    </view>
                </view>
                <view class="flexr order_count">
                    <view class="p_pirce">{{globalData.appConfig && globalData.appConfig.currency && globalData.appConfig.currency.symbol || '￥'}}{{ objInfo.orderprice }}</view>
                    <view class="p_count">x{{ objInfo.procount }}</view>
                    <view class="pstate" v-if="objInfo.orderstate == 8 || objInfo.orderstate == 9">{{ objInfo.orderstate == 8 ? '退款中' : '退款成功' }}</view>
                </view>
            </view>
            <view class="order-pay flexbox" v-if="objInfo.orderprice > 0">
                <view>实付：</view><view class="pay_price">{{ globalData.appConfig && globalData.appConfig.currency && globalData.appConfig.currency.symbol || '￥'  }}<view class="b">{{ (objInfo.orderprice.toString()).split('.')[0] }}<text v-if="(objInfo.orderprice.toString()).includes('.')">.{{ (objInfo.orderprice.toString()).split('.')[1] }}</text></view></view>
            </view>
        </view>
        <view class="orderBtns">
            <template v-if="objInfo.orderstate == '0'">
                <view class="btn cancel" @click="toLink('cancel')">取消订单</view>
                <view class="btn contact"  @click="toLink('contact')">联系商家</view>
                <view class="btn pay" @click="toLink('pay')">去付款</view>
            </template>
            <template v-else-if="objInfo.orderstate == '1'">
                <view class="btn todetail" @click="toLink('cancel',1)" v-if="objInfo.homemakingtype == 0">取消预约</view>
                <view class="btn todetail" @click="toLink('cancel_yuyue')" v-else-if="objInfo.refundnumber == 0 ||objInfo.refundnumber == 1 ">{{ objInfo.refundnumber == 0 ? "申请退款":"修改申请" }}</view>
                <view class="btn todetail" @click="toLink('cancel_yuyue')" v-else-if="objInfo.refundnumber == 2">查看退款详情</view>
                <view class="btn contact"  @click="toLink('contact')">联系商家</view>
            </template>
            <template v-else-if="objInfo.orderstate == '2'">
                <view class="btn contact"  @click="toLink('contact')">联系商家</view>
                <view class="btn todetail" @click="toLink('cancel',1)" v-if="objInfo.homemakingtype == 0">取消预约</view>
                <template v-else-if="objInfo.cardnum && objInfo.cardnum.length">
                    <view class="btn code pay"  @click="toLink('code')">服务码</view> 
                </template>
            </template>
            <template v-else-if="objInfo.orderstate == '3'">
                <view class="btn del" @click="toLink('del')">删除订单</view>
            </template>
            <template v-else-if="objInfo.orderstate == '4'">
                <view class="btn contact"  @click="toLink('contact')">联系商家</view>
                <view class="btn del" @click="toLink('cancelservice')" v-if="objInfo.homemakingtype == 2">申请售后</view>
                <view class="btn del" @click="toLink('cancel',1)" v-else-if="objInfo.homemakingtype == 0">取消预约</view>
            </template>
            <template v-else-if="objInfo.orderstate == '5'">
                <view class="btn contact borderBtn "  @click="toLink('contact')">联系商家</view>
                <view class="btn pay " @click="toLink('yanshou')">验收完成</view>
            </template>
           
            <template v-else-if="objInfo.orderstate == '7' || objInfo.orderstate == '9'">
                <view class="btn del"  @click="toLink('del')">删除订单</view>
            </template>
            <template v-else-if="objInfo.orderstate == '11' || objInfo.orderstate == '6'">
                <view class="btn aftersale" v-if="objInfo.aftersale == 1"  @click="toLink('aftersale')">售后维保</view>
                <view class="btn del" v-else @click="toLink('del')">删除订单</view>
                <view class="btn contact"  @click="toLink('contact')">联系商家</view>
            </template>
           
        </view>
    </view>
</template>

<script>
const app = getApp();
export default {
    props:{
            objInfo:{
                type:Object,
                default:{
                    
                }
            },
            ind:{
                type:Number,
                default:0
            }
        },
        computed:{
            getState(){
                return function(order){
                    const that = this;
                    let orderstate = order.orderstate;
                    let stateText = '';
                    switch(orderstate){
                        case '0':
                            stateText = '待付款';
                            break;
                        case '1':
                            stateText = '待商家确认';
                            break;
                        case '2':
                            stateText = '待服务';
                            break;
                        case '3':
                            stateText = '服务无效';
                            break;
                        case '4':
                            stateText = '待服务';
                            break;
                        case '5':
                            stateText = '待验收';
                            break;
                        case '6':
                            stateText = '服务完成';
                            break;
                        case '7':
                            stateText = '已取消';
                            break;
                        case '8':
                            stateText = '交易关闭';
                            break;
                        case '9':
                            stateText = '已取消';
                        case '11':
                            stateText = '服务完成';
                            break;
                    }
                    console.log(stateText)
                    return stateText
                }
            },

            // 时间戳转换
            timeTrans(){
                return function(order){
                    let timestr = order.doortime_date
                    const dateFormatter = app.dateFormatter(timestr);
                    const year = dateFormatter.year;
                    const month = dateFormatter.month;
                    const day = dateFormatter.day;
                    // const hour = dateFormatter.hour;
                    // const minute = dateFormatter.minute;
                    // const second = dateFormatter.second;
                    
                    let today_dateFormatter = app.dateFormatter(parseInt(new Date().valueOf() / 1000));
                    const today_year = today_dateFormatter.year;
                    // const today_month = today_dateFormatter.month;
                    let showStr = '';
                    if (new Date(timestr * 1000).toDateString() === new Date().toDateString()) {
                        showStr = '今天 ' + order.doortime_time 
                    }else  if(year == today_year){
                        showStr = month + '月' + day + '日 '  + order.doortime_time 
                    }else{
                        showStr = year + '/' + month + '/' + day + ' '  ;

                    }
                    return showStr
                }
            },
        },
        data(){
            return {
                globalData:app.globalData,
                imgVersion:app.globalData.imgVersion,
                imgpath:app.globalData.imgPath + '/member/order/',
            }
        },
        mounted(){
        },
        methods:{
            // 点击页面跳转 
            /***
             * @param {*} type 跳转类型  order => 详情   
             */
             toLink(type,is_yuyue = 0){
                const that = this;
                let url = ''
                switch(type){
                    case 'pay':
                        uni.$emit('payorder',{objInfo:this.objInfo,ind:this.ind})
                        break;
                    case 'cancel':
                        uni.$emit('cancelorder',{objInfo:this.objInfo,ind:this.ind,is_yuyue:is_yuyue})
                        break;
                    case 'order':
                        url = `${app.globalData.appConfig && app.globalData.appConfig.cfg_user_index }/orderdetail-homemaking-${that.objInfo.id}.html`
                        app.linkTo(url,false,false)
                        break;
                    case 'rebuy':
                        url = `${app.globalData.appConfig && app.globalData.appConfig.cfg_user_index }/orderdetail-homemaking-${that.objInfo.id}.html`
                        app.linkTo(url,false,false)
                        break;
                    case 'del':
                        uni.$emit('delorder',{objInfo:this.objInfo,ind:this.ind})
                        break;
                    case 'aftersale':
                        url = `${app.globalData.appConfig && app.globalData.appConfig.cfg_user_index }/homemaking-repair-${that.objInfo.id}.html`
                        app.linkTo(url,false,false)
                        break;
                    case 'cancelservice':
                        url = `${app.globalData.appConfig && app.globalData.appConfig.cfg_user_index }/homemaking-cancelservice-${that.objInfo.id}.html`
                        app.linkTo(url,false,false)
                        break;
                    case 'yanshou':
                    case 'code':
                        // 确认验收
                        uni.$emit('showwuliu',{objInfo:this.objInfo,ind:this.ind,opt:type == 'yanshou' ? 'confirm' : 'showCode'})
                        break;
                    case 'cancel_yuyue':
                        // 取消预约
                        if(that.objInfo.homemakingtype == 0){

                        }else{
                            url = `${app.globalData.appConfig.cfg_user_index}/homemaking-cancel-${that.objInfo.id}.html`;
                            app.linkTo(url,false,false)
                        }
                        break;
                    case 'contact':
                        // 联系商家
                        if(app.globalData.appBoolean && app.globalData.platForm =='web'){
                            location.href = 'tel:' + that.objInfo.tel 
                        }else{
                            uni.makePhoneCall({
                                phoneNumber: that.objInfo.tel,
                                success: (result) => {},
                                fail: (error) => {}
                            })
                        }
                        break;
                }
            }
        }
}
</script>

<style scoped>
 @import url(./orderlist_homemaking.css);
</style>